/*
* support3
*/
package com.pitop.frontend.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.enums.FileEnum;
import com.pitop.core.enums.LanguageEnum;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.FrontendI18nException;
import com.pitop.core.tools.FileUtil;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.frontend.entity.FrontendI18n;
import com.pitop.frontend.entity.FrontendI18nCategory;
import com.pitop.frontend.entity.FrontendI18nCategoryState;
import com.pitop.frontend.entity.FrontendI18nState;
import com.pitop.frontend.service.FrontendI18nCategorySV;
import com.pitop.frontend.service.FrontendI18nSV;
import com.pitop.frontend.vo.FrontendI18nVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 前端国际化 控制器
 *
 * @author support3
 */
@RestController
@RequestMapping("/frontendI18n")
@Slf4j
@Api(value = "前端国际化控制器", description = "前端国际化控制器")
public class FrontendI18nCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private FrontendI18nSV frontendI18nSV;

    @Resource
    private FrontendI18nCategorySV frontendI18nCategorySV;

    /**
     * 查询FrontendI18n一个详情信息
     *
     * @param id   id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询FrontendI18n一个详情信息", notes = "查询FrontendI18n一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public FrontendI18n load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        FrontendI18n frontendI18n = frontendI18nSV.load(id, code);
        log.info(JSON.toJSONString(frontendI18n));
        return frontendI18n;
    }


    /**
     * 根据条件id查询FrontendI18n一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询FrontendI18n一个详情信息", notes = "查询FrontendI18n一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public FrontendI18n loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        FrontendI18n frontendI18n = frontendI18nSV.loadById(id);
        log.info(JSON.toJSONString(frontendI18n));
        return frontendI18n;
    }

    /**
     * 根据条件code查询FrontendI18n一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询FrontendI18n一个详情信息", notes = "查询FrontendI18n一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        FrontendI18n frontendI18n = frontendI18nSV.loadByCode(code);
        log.info(JSON.toJSONString(frontendI18n));
        return BeanRet.create(true, "查询成功", frontendI18n);
    }

    /**
     * 根据条件语言查询FrontendI18n一个详情信息
     *
     * @param language 语言
     * @return BeanRet
     */
    @ApiOperation(value = "根据语言查询全部国际化数据", notes = "根据语言查询全部国际化数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/{language}")
    @ResponseBody
    public String watchByLanguage(@PathVariable String language) {
        if (StringTools.isEmpty(language)) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        return frontendI18nSV.watchByLanguage(language, FrontendI18nState.Enable);
    }


    /**
     * 查询FrontendI18n信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询FrontendI18n信息集合", notes = "查询FrontendI18n信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fronendCatetoryCode", value = "分类id", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言", paramType = "query"),
            @ApiImplicitParam(name = "i18nKey", value = "国际化key", paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore FrontendI18n frontendI18n, Integer curPage, Integer pageSize) {
        Page<FrontendI18n> page = new Page<>(pageSize, curPage);
        if(StringTools.isEmpty(frontendI18n.getLanguage())) {
            frontendI18n.setLanguage(LanguageEnum.zh_CN.name());
        }
        List<FrontendI18n> frontendI18ns = frontendI18nSV.list(frontendI18n, page.genRowStart(), page.getPageSize());
        String i18nKey ;
        String language ;
        List<FrontendI18n> frontendI18nList;
        for (FrontendI18n frontendIl18nFlag: frontendI18ns) {
            i18nKey = frontendIl18nFlag.getI18nKey();
            language = frontendIl18nFlag.getLanguage();
            frontendI18nList = frontendI18nSV.ListByI18nKey(i18nKey, language);
            frontendIl18nFlag.setFrontendI18nVOs(StringTools.convertList(frontendI18nList, FrontendI18nVO.class));
            frontendIl18nFlag.setFrontendCategory(frontendI18nCategorySV.loadByCode(frontendIl18nFlag.getFronendCatetoryCode()).getName());
        }
        page.setVoList(frontendI18ns);
        int total = frontendI18nSV.count(frontendI18n);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计FrontendI18n信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计FrontendI18n信息数量", notes = "统计FrontendI18n信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "fronendCatetoryCode", value = "前端国际化分类编码", paramType = "query"),
            @ApiImplicitParam(name = "i18nKey", value = "国际化键", paramType = "query"),
            @ApiImplicitParam(name = "i18nValue", value = "国际化文本内容", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1013)： 启用 Enable,禁用 Disable", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore FrontendI18n frontendI18n) {
        return frontendI18nSV.count(frontendI18n);
    }


    /**
     * 创建FrontendI18n
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建FrontendI18n", notes = "创建FrontendI18n")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fronendCatetoryCode", value = "前端国际化分类编码", paramType = "query"),
            @ApiImplicitParam(name = "i18nKey", value = "国际化键", paramType = "query"),
            @ApiImplicitParam(name = "i18nValue", value = "国际化文本内容", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1013)： 启用 Enable,禁用 Disable", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@RequestBody  FrontendI18n frontendI18n) {
        frontendI18nSV.save(frontendI18n);
        return BeanRet.create(true, "添加或修改成功");
    }

    /**
     * 上传json文件，批量导入数据
     * 该方法暂时未使用：批量导入时无法确定所属前端国际化分类
     *
     * @return BeanRet
     */
    @ApiOperation(value = "上传json文件，创建前端国际化", notes = "上传json文件，批量导入数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fronendCatetoryCode", value = "前端国际化分类编码", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言", paramType = "query"),
    })
    @PostMapping("/create")
    @ResponseBody
    @Deprecated
    public FrontendI18n create(String fronendCatetoryCode, String language, MultipartFile limitFile) {
        String fileType = FileEnum.getFileType(limitFile.getContentType()).name();
        try {
            InputStream inputStream = limitFile.getInputStream();
            String str = FileUtil.fileToString(inputStream);
            JSONArray jsonArray = JSONArray.parseArray(str);
            JSONObject jsonObject;
            FrontendI18n frontendI18n;
            for (int i = 0; i < jsonArray.size(); i++) {
                frontendI18n = new FrontendI18n();
                jsonObject = (JSONObject) jsonArray.get(i);
                frontendI18n.setFronendCatetoryCode(fronendCatetoryCode);
                frontendI18n.setLanguage(language);
            }
            System.out.print(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 修改前端国际化对象
     * 1.验证数据合法性
     * ****非空验证
     * ****分类编码正确性验证
     * ****某种语言下，国际化键唯一
     * 2.修改数据
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改FrontendI18n", notes = "修改FrontendI18n")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "fronendCatetoryCode", value = "前端国际化分类编码", paramType = "query"),
            @ApiImplicitParam(name = "i18nKey", value = "国际化键", paramType = "query"),
            @ApiImplicitParam(name = "i18nValue", value = "国际化文本内容", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1013)： 启用 Enable,禁用 Disable", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore FrontendI18n frontendI18n) {
        //1.数据合法性验证：非空验证
        if (frontendI18n == null) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(frontendI18n.getFronendCatetoryCode())) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(frontendI18n.getI18nKey())) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(frontendI18n.getI18nValue())) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(frontendI18n.getLanguage())) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //分类编码是否正确
        FrontendI18nCategory frontendI18nCategory = frontendI18nCategorySV.load(frontendI18n.getFronendCatetoryCode(), FrontendI18nCategoryState.Enable.name());
        if (frontendI18nCategory == null) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //某种语言下，国际化键是否存在
        FrontendI18n frontendI18nFlag = frontendI18nSV.load(frontendI18n.getI18nKey(), frontendI18n.getLanguage());
        if (frontendI18nFlag != null && !frontendI18n.getCode().equals(frontendI18nFlag.getCode())) {
            throw new FrontendI18nException(BaseException.BaseExceptionEnum.Exists);
        }
        frontendI18nSV.update(frontendI18n);
        return BeanRet.create(false, "修改成功");
    }



    /**
     * 查询FrontendI18n信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询FrontendI18n信息集合", notes = "查询FrontendI18n信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/listToCreateCode")
    @ResponseBody
    public BeanRet listToCreateCode(@ApiIgnore FrontendI18n frontendI18n, Integer curPage, Integer pageSize) {
        Page<FrontendI18n> page = new Page<>(pageSize, curPage);
        List<FrontendI18n> frontendI18ns = frontendI18nSV.list(frontendI18n, page.genRowStart(), page.getPageSize());
        for (FrontendI18n frontendI18nFlag: frontendI18ns) {
            frontendI18nSV.updateToCreateCode(frontendI18nFlag);
        }
        return BeanRet.create(true, "修改成功", page);
    }

}
